package com.lagou.server;

import com.lagou.model.User;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class ServerUserDaoImpl implements ServerUserDao {


    private List<User> userList = new ArrayList<>();
    private ObjectInputStream objectInputStream ;

    {
        try {
            File file = new File("D:/accountInformation.txt");
            if (file.length()!=0){
                objectInputStream = new ObjectInputStream(new FileInputStream("D:/accountInformation.txt"));
                userList = (List<User>) objectInputStream.readObject();
            }else {
                userList = new ArrayList();
            }

        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        }

    }

    /**
     * 编程实现管理员账号和密码的校验并将结果返回出去
     * @param user
     * @return
     */
    public boolean ServerManagerCheck(User user){
        if ("admin".equals(user.getStudentId()) && "123456".equals(user.getPassword())){
            return true;
        }
        return false;
    }



    /**
     * 编程实现学员账号和密码的校验并将结果返回出去
     * @param user
     * @return
     */
    public boolean ServerUserCheck(User user) throws IOException, ClassNotFoundException {
        for (User user1 : userList) {
            if (user1.getStudentId().equals(user.getStudentId()) && user1.getPassword().equals(user.getPassword())){
                return true;
            }
        }
        return false;
    }

    @Override
    public boolean ServerUpdateUserPassword(String studentId,User user) {
        for (int i = 0; i < userList.size(); i++) {
            if (userList.get(i).getStudentId().equals(studentId)){
                userList.get(i).setPassword(user.getPassword());
                return true;
            }
        }
        return false;
    }

    /**
     * 管理员新增学员信息
     * @param user
     * @return
     * @throws IOException
     * @throws ClassNotFoundException
     */
    public boolean ServerInsertStudent(User user) throws IOException, ClassNotFoundException {
        System.out.println(user.getUserName());
        if (userList.size()<1) {
            userList.add(user);
            return true;
        } else {
            for (User user1 : userList) {
                if (user1.getStudentId().equals(user.getStudentId())){
                    return false;
                }else {

                    userList.add(user);
                    return true;
                }
            }
        }
        return false;
    }

    /**
     * 根据学号查询学生信息
     * @param studentId
     * @return
     */
    public User ServerSelectUserByStudentId(String studentId){
        for (User user : userList) {
            if (user.getStudentId().equals(studentId)){
                return user;
            }
        }
        return null;
    }

    @Override
    public List<User> ServerSelectAllUser() {
        if (userList.size()<0 || null ==userList ){
            return null;
        }
        return userList;
    }

    @Override
    public boolean ServerDeleteUserByStudentId(String studentId) {
        for (User user : userList) {
            if (user.getStudentId().equals(studentId)){
                userList.remove(user);
                return true;
            }
        }
        return false;
    }

    @Override
    public boolean ServerUpdateUserByStudentId(String studentId,User user) {
        for (int i = 0; i < userList.size(); i++) {
            if (userList.get(i).getStudentId().equals(studentId)){
                userList.get(i).setStudentId(user.getStudentId());
                userList.get(i).setUserName(user.getUserName());
                userList.get(i).setPassword(user.getPassword());
                return true;
            }
        }

        return false;
    }


    /**
     * 退出时将数据保存至文件
     * @throws IOException
     */
    public void closeSavaData() throws IOException {
        File file = new File("D:/accountInformation.txt");
        if(!file.exists()){
            file.createNewFile();
        }
        ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream("D:/accountInformation.txt"));
        objectOutputStream.writeObject(userList);
    }
}
